package com.bbjob.service.impl;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bbjob.dto.Result;
import com.bbjob.model.QueChoice;
import com.bbjob.model.QueOperation;
import com.bbjob.service.IQuestionBankService;
import com.bbjob.toolutil.DataEntity;
import com.bbjob.toolutil.HttpClientUtil;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

@Service
public class QuestionBankServiceImpl implements IQuestionBankService {

	@Value("${sys.uniquecode}")
	private String uniquecode;
	@Value("${examol.center.url}")
	private String centerUrl;
/*	@Value("${examol.center.simple.url}")
	private String centerSimpleUrl;*/
	@Value("${examol.center.que.bank.url}")
	private String centerQueBankUrl;
	@Value("${examol.center.que.freeBank.url}")
	private String centerQueFreeBankUrl;
	@Value("${examol.center.que.choice.url}")
	private String centerQueChoiceUrl;
	@Value("${examol.center.que.freeChoice.url}")
	private String centerQueFreeChoiceUrl;
	@Value("${examol.center.que.operation.url}")
	private String centerQueOperationUrl;
	@Value("${examol.center.que.freeOperation.url}")
	private String centerQueFreeOperationUrl;
	@Value("${examol.center.que.itemByChoice.url}")
	private String centerQueItemByChoiceUrl;
	@Value("${examol.center.que.choiceByBank.url}")
	private String centerQueChoiceByBankUrl;
	@Value("${examol.center.que.operationByBank.url}")
	private String centerQueOperationByBankUrl;
	@Value("${examol.center.exam.paper.all}")
	private String centerExamPaperAll;
	@Value("${examol.center.que.choiceAnalysis.url}")
	private String centerQueChoiceAnalysisUrl;
	@Value("${examol.center.que.operationAnalysis.url}")
	private String centerQueOperationAnalysisUrl;
	
	@Override
	public DataEntity getBank(Integer type,String name,Integer difficulty, Integer userid, Integer page, Integer size) {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (type != null) {
				params.put("type", type.toString());
			}
			if (name != null && !"".equals(name)) {
				params.put("name", name);
			}
			if (difficulty != null) {
				params.put("difficulty", difficulty.toString());
			}
			if (userid != null) {
				params.put("userid", userid.toString());
			}
			
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueBankUrl, params);
			
		    GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

	@Override
	public DataEntity getFreeBank(Integer type,String name,Integer difficulty, Integer page, Integer size) {
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (type != null) {
				params.put("type", type.toString());
			}
			if (name != null && !"".equals(name)) {
				params.put("name", name);
			}
			if (difficulty != null) {
				params.put("difficulty", difficulty.toString());
			}
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueFreeBankUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

	@Override
	public DataEntity getChoice(String bankid, String tag, String name, Integer type, Integer userid,
			Integer page, Integer size, Integer bankType) {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			if (bankid != null && !"".equals(bankid)) {
				params.put("bankid", bankid);
			}
			if (tag != null && !"".equals(tag)) {
				params.put("tag", tag);
			}
			if (name != null && !"".equals(name)) {
				params.put("name", name);
			}
			if (type != null) {
				params.put("type", type.toString());
			} 
			if (userid != null) {
				params.put("userid", userid.toString());
			} 
			if (bankType != null) {
				params.put("bankType", bankType.toString());
			}
			
			
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueChoiceUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
		    DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public DataEntity getFreeChoice(String bankid, String tag, String name, Integer type, Integer userid,
			Integer page, Integer size, Integer bankType) {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			if (bankid != null && !"".equals(bankid)) {
				params.put("bankid", bankid);
			}
			if (tag != null && !"".equals(tag)) {
				params.put("tag", tag);
			}
			if (name != null && !"".equals(name)) {
				params.put("name", name);
			}
			if (type != null) {
				params.put("type", type.toString());
			} 
			if (userid != null) {
				params.put("userid", userid.toString());
			} 
			if (bankType != null) {
				params.put("bankType", bankType.toString());
			}
			
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueFreeChoiceUrl, params);
			GsonBuilder builder = new GsonBuilder();
			
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public DataEntity getOperation(String tag, String bankid, String name, Integer type, Integer userid,
			Integer page, Integer size) {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			if (bankid != null && !"".equals(bankid)) {
				params.put("bankid", bankid);
			}
			if (tag != null && !"".equals(tag)) {
				params.put("tag", tag);
			}
			if (name != null && !"".equals(name)) {
				params.put("name", name);
			}
			if (type != null) {
				params.put("type", type.toString());
			} 
			if (userid != null) {
				params.put("userid", userid.toString());
			} 
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueOperationUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public DataEntity getFreeOperation(String tag, String bankid, String name, Integer type,
			Integer userid, Integer page, Integer size) {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			if (bankid != null && !"".equals(bankid)) {
				params.put("bankid", bankid);
			}
			if (tag != null && !"".equals(tag)) {
				params.put("tag", tag);
			}
			if (name != null && !"".equals(name)) {
				params.put("name", name);
			}
			if (type != null) {
				params.put("type", type.toString());
			} 
			if (userid != null) {
				params.put("userid", userid.toString());
			} 
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueFreeOperationUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public DataEntity getChoiceByBank(QueChoice choice, Integer page, Integer size) {

		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (choice.getType() != null && !"".equals(choice.getType())) {
				params.put("type", choice.getType());
			}
			
			if (choice.getTitle() != null && !"".equals(choice.getTitle())) {
				params.put("title", choice.getTitle());
			}
			
			if (choice.getTagName() != null && !"".equals(choice.getTagName())) {
				params.put("tagName", choice.getTitle());
			}
			
			if (choice.getBankId() != null && !"".equals(choice.getBankId())) {
				params.put("bankId", choice.getBankId());
			}
			
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueChoiceByBankUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

	@Override
	public DataEntity getItemByChoiceid(String Choiceid) {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (Choiceid != null && !"".equals(Choiceid)) {
				params.put("Choiceid", Choiceid);
			}
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueItemByChoiceUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

	@Override
	public DataEntity getOperationByBank(QueOperation operation, Integer page, Integer size) {

		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (operation.getType() != null) {
				params.put("type", operation.getType().toString());
			}
			
			if (operation.getContent() != null && !"".equals(operation.getContent())) {
				params.put("content", operation.getContent());
			}
			
			if (operation.getTagName() != null && !"".equals(operation.getTagName())) {
				params.put("tagName", operation.getTagName());
			}
			
			if (operation.getBankId() != null && !"".equals(operation.getBankId())) {
				params.put("bankId", operation.getBankId());
			}
			
			params.put("page", page.toString());
			params.put("size", size.toString());
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueOperationByBankUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

	@Override
	public Result selectAll(Integer difficultId, Integer userId) {

		Map<String, Object> params = new HashMap<String, Object>();
		
		try {
			params.put("organCode", uniquecode);

			if (difficultId != null) {
				params.put("difficultyId", difficultId);
			}
			
			if (userId != null) {
				params.put("createrId", userId);
			}
			String questionBankStr = HttpClientUtil.doPost(centerUrl + centerExamPaperAll, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			Result entity = builder.create().fromJson(questionBankStr, Result.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new Result(8106, "出错了");
		}
	}

	@Override
	public DataEntity getChoiceAnalysis(String id) {

		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (id != null && !"".equals(id)) {
				params.put("id", id);
			}
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueChoiceAnalysisUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

	@Override
	public DataEntity getOperationAnalysis(String id) {

		Map<String, String> params = new HashMap<String, String>();
		
		try {
			params.put("uniquecode", uniquecode);
			
			if (id != null && !"".equals(id)) {
				params.put("id", id);
			}
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerQueOperationAnalysisUrl, params);
			
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			DataEntity entity = builder.create().fromJson(resultStr, DataEntity.class);
			
			return entity;
		} catch(Exception e) {
			e.printStackTrace();
			return new DataEntity("8106", false, "出错了");
		}
	}

}
